package com.big.common.mybatis.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.big.common.saToken.domain.dto.LoginDTO;
import com.big.common.saToken.util.CurrentUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * <p>
 * MybatisPlus 自动填充配置
 * </p>
 *
 * @author 山归岿
 * @create 2025-06-21-19:00
 */
@Slf4j
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {
    /**
     * 自动填充创建时间
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", LocalDateTime::now, LocalDateTime.class);
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class);
        LoginDTO currentUser = CurrentUserUtil.getCurrentUser();
        // 可以在这里获取当前登录用户的信息，填充创建人等字段
        this.strictInsertFill(metaObject, "createBy", String.class, CurrentUserUtil.getCurrentUserId());
    }

    /**
     * 自动填充更新时间
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class);
        // 可以在这里获取当前登录用户的信息，填充更新人等字段
        this.strictUpdateFill(metaObject, "updateBy", String.class, CurrentUserUtil.getCurrentUserId());
    }
}
